from typing import Optional, List
from pydantic import BaseModel, Field
from datetime import datetime
from .base import BaseSchema


class ModelVersionBase(BaseSchema):
    """模型版本基础模型"""
    version_name: str = Field(..., max_length=100, description="版本名称(如V1.0基础版、V1.1优化版)")
    version_code: str = Field(..., max_length=50, description="版本号(如1.0.0)")
    file_path: Optional[str] = Field(None, max_length=500, description="模型文件路径")
    accuracy: Optional[str] = Field(None, max_length=50, description="准确率")
    recall: Optional[str] = Field(None, max_length=50, description="召回率")
    description: Optional[str] = Field(None, description="版本描述")
    is_active: bool = Field(default=True, description="是否激活")


class ModelVersionCreate(ModelVersionBase):
    """创建模型版本请求模型"""
    model_id: int = Field(..., description="模型ID")


class ModelVersionUpdate(BaseModel):
    """更新模型版本请求模型"""
    version_name: Optional[str] = Field(None, max_length=100, description="版本名称")
    version_code: Optional[str] = Field(None, max_length=50, description="版本号")
    file_path: Optional[str] = Field(None, max_length=500, description="模型文件路径")
    accuracy: Optional[str] = Field(None, max_length=50, description="准确率")
    recall: Optional[str] = Field(None, max_length=50, description="召回率")
    description: Optional[str] = Field(None, description="版本描述")
    is_active: Optional[bool] = Field(None, description="是否激活")


class ModelVersionSchema(ModelVersionBase):
    """模型版本响应模型"""
    id: int
    model_id: int
    created_at: datetime
    updated_at: datetime
    
    class Config:
        from_attributes = True


class DetectionModelBase(BaseSchema):
    """检测模型基础模型"""
    name: str = Field(..., max_length=200, description="模型名称")
    description: Optional[str] = Field(None, description="模型描述")
    format: str = Field(..., max_length=50, description="模型格式(如YOLOv8、Mask R-CNN)")
    application_scenario: str = Field(..., description="适用场景(如管道泄漏、电力线破损)")
    input_requirements: Optional[str] = Field(None, description="输入要求(图片/视频分辨率)")
    output_format: Optional[str] = Field(None, description="输出格式(坐标+置信度)")
    is_active: bool = Field(default=True, description="是否激活")


class DetectionModelCreate(DetectionModelBase):
    """创建检测模型请求模型"""
    pass


class DetectionModelUpdate(BaseModel):
    """更新检测模型请求模型"""
    name: Optional[str] = Field(None, max_length=200, description="模型名称")
    description: Optional[str] = Field(None, description="模型描述")
    format: Optional[str] = Field(None, max_length=50, description="模型格式")
    application_scenario: Optional[str] = Field(None, description="适用场景")
    input_requirements: Optional[str] = Field(None, description="输入要求")
    output_format: Optional[str] = Field(None, description="输出格式")
    current_version_id: Optional[int] = Field(None, description="当前使用的版本ID")
    is_active: Optional[bool] = Field(None, description="是否激活")


class DetectionModelSchema(DetectionModelBase):
    """检测模型响应模型"""
    id: int
    current_version_id: Optional[int]
    current_version: Optional[ModelVersionSchema] = None
    created_at: datetime
    updated_at: datetime
    
    class Config:
        from_attributes = True


class DetectionModelWithVersions(DetectionModelSchema):
    """带版本信息的检测模型响应模型"""
    versions: List[ModelVersionSchema] = []